package com.client.ryt.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.net.ParseException;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 系统级别工具
 */
public class SysUtil {

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }



    public static String getTimeDiff(Date date,String format){
        return formatDate(date,format);
    }

    public static String getTimeDiff(Date date) {
        return formatDate(date,"yyyy-MM-dd HH:mm");
    }

    public static String formatDate(Long data,String format){
        return formatDate(new Date(data),format);
    }


    public static String formatDate(Date date,String format){
        if(date == null) return "";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static Date formatDate(String dateString,String format){
        SimpleDateFormat formater= new SimpleDateFormat(format);
        Date date = null;
        try{
            try {
                date = formater.parse(dateString);
            } catch (java.text.ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }catch(ParseException e){
            e.printStackTrace();
        }
        return date;
    }

    public static  int getSysVersion(){
        int version;
        try {
            version = Integer.valueOf(android.os.Build.VERSION.SDK);
        } catch (NumberFormatException e) {
            version = 0;
        }
        return version;
    }

    /**
     * 用来判断服务是否后台运行
     * @param
     * @param className 判断的服务名字
     * @return true 在运行 false 不在运行
     */
    public static boolean isServiceRunning(Context mContext,String className) {
        boolean IsRunning = false;
        ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceList   = activityManager.getRunningServices(30);
        if (!(serviceList.size()>0)) {
            return false;
        }
        for (int i=0; i<serviceList.size(); i++) {
            if (serviceList.get(i).service.getClassName().equals(className) == true) {
                IsRunning = true;
                break;
            }
        }
        return IsRunning ;
    }
}
